package com.abkj.platform.tail.controller;

import com.abkj.platform.tail.domain.Osmometer;
import com.abkj.platform.tail.service.BaseService;
import com.abkj.platform.tail.util.DateUtil;
import com.abkj.platform.tail.util.JsonUtils;
import com.abkj.platform.tail.vo.DataVO;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "history" })
public class HistoryController extends BaseController {
	@RequestMapping({ "/rain" })
	public String rain(ModelMap model) {
		String today = DateUtil.dateToStr(new Date(), "yyyy/MM/dd");
		model.put("today", today);
		return "/history/rain";
	}

	@RequestMapping(value = { "/raindata" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String rainData(int condition, String time) {
		System.out.println("condition=" + condition + "&time=" + time);
		Map ret = parseRainData(time, condition);
		return JsonUtils.writeObjToString(ret);
	}

	private Map<String, Object> parseRainData(String time, int condition) {
		int date = NumberUtils.toInt(time.replaceAll("/", ""));
		Map ret = new HashMap();
		List<Float> xs = new ArrayList();
		int daysOfMonth = daysOfMonth(time);
		float max = 0.0F;
		float min = 0.0F;
		float allday = 0.0F;
		int date02;
		if (condition == 1) {
			List rains = this.baseService.findBySql("select * from raindata where day=" + date);
			if (rains.size() > 0) {
				Map map = (Map) rains.get(0);
				String allVal = String.valueOf(map.get("allval"));
				allday = NumberUtils.toFloat(String.valueOf(map.get("val")));
				int len = allVal.length() / 4;
				for (int i = 0; i < 24; i++)
					if (i < len) {
						String tmp = allVal.substring(i * 4, i * 4 + 4);
						xs.add(Float.valueOf(Integer.parseInt(tmp, 16) * 0.1F));
					} else {
						xs.add(null);
					}
			} else {
				for (int i = 0; i < 24; i++)
					xs.add(null);
			}
		} else {
			int date01 = date * 100;
			date02 = date * 100 + 31;
			List<Map> rains = this.baseService.findBySql(
					"select * from raindata where day>=" + date01 + " and day<=" + date02 + " order by createtime");
			int len = rains.size();
			boolean hasData = false;
			for (int i = 1; i <= daysOfMonth; i++) {
				for (Map map : rains) {
					String days = String.valueOf(map.get("day"));
					int day = NumberUtils.toInt(days.substring(days.length() - 2, days.length()));
					if (day == i) {
						int v = NumberUtils.toInt(String.valueOf(map.get("val")));
						xs.add(Float.valueOf(v * 0.1F));
						hasData = true;
						break;
					}
					if (day > i) {
						hasData = false;
						break;
					}
					hasData = false;
				}

				if (!hasData) {
					xs.add(null);
				}
			}
		}

		for (Float x : xs) {
			if (x != null) {
				if (x.floatValue() > max) {
					max = x.floatValue();
				}
				if (x.floatValue() < min)
					min = x.floatValue();
			}
		}
		ret.put("val", Double.valueOf(allday * 0.1D));
		ret.put("max", Float.valueOf(max));
		ret.put("min", Float.valueOf(min));
		ret.put("xs", xs);
		ret.put("days", Integer.valueOf(daysOfMonth));
		return ret;
	}

	public int daysOfMonth(String date) {
		String[] dates = date.split("/");
		Calendar c = Calendar.getInstance();
		c.set(1, NumberUtils.toInt(dates[0]));
		c.set(2, NumberUtils.toInt(dates[1]) - 1);
		int days = c.getActualMaximum(5);
		return days;
	}

	@RequestMapping({ "/ultrasonic" })
	public String ultrasonic(ModelMap model) {
		return "/history/ultrasonic";
	}

	@RequestMapping(value = { "/ultrasonicdata" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String ultrasonicData(String begin, String end) {
		String basicQuery = "SELECT * FROM ultrasonic limit 1";
		List basic = this.baseService.findBySql(basicQuery);
		float height = 0.0F;
		float min = 0.0F;
		float max = 0.0F;
		if ((basic != null) && (basic.size() != 0)) {
			Map basicU = (Map) basic.get(0);
			height = NumberUtils.toFloat(String.valueOf(basicU.get("height")));
		}

		String beginTime = begin + " 00:00:00";
		String endTime = end + " 23:59:59";
		List<Map> ultrasonics = this.baseService.findBySql("select * from ultrasonicdata where createtime>='" + beginTime
				+ "' and createtime<='" + endTime + "' order by createtime");
		Map ret = new HashMap();
		List datas = new ArrayList();
		if ((ultrasonics != null) && (ultrasonics.size() > 0)) {
			Map tmp = (Map) ultrasonics.get(0);
			max = min = height - NumberUtils.toFloat(String.valueOf(tmp.get("should")));
		}
		for (Map map : ultrasonics) {
			long x = DateUtil.strToLong(String.valueOf(map.get("createtime")), "yyyy-MM-dd HH:mm:ss");
			float y = NumberUtils.toFloat(String.valueOf(map.get("should")));
			float val = height - y;
			if (max < val) {
				max = val;
			}
			if (min > val) {
				min = val;
			}
			DataVO dv = new DataVO();
			dv.setX(x);
			dv.setY(val);
			datas.add(dv);
		}
		ret.put("data", datas);
		ret.put("min", Float.valueOf(min));
		ret.put("max", Float.valueOf(max));
		return JsonUtils.writeObjToString(ret);
	}

	@RequestMapping({ "/level" })
	public String level(ModelMap model) {
		List levels = this.baseService.findBySql("SELECT * FROM LEVEL WHERE TYPE=0");
		model.put("levels", levels);
		return "/history/level";
	}

	@RequestMapping(value = { "/leveldata" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String levelData(int senid, String begin, String end) {
		String beginTime = begin + " 00:00:00";
		String endTime = end + " 23:59:59";
		System.out.println("senid=" + senid + "&beginTime=" + beginTime + "&endTime=" + endTime);
		String sql = "select * from leveldata where sid=" + senid + " and createtime>='" + beginTime
				+ "' and createtime<='" + endTime + "' order by createtime";
		List datas = this.baseService.findBySql(sql);
		Map ret = createData(2, datas, 0.0F);
		return JsonUtils.writeObjToString(ret);
	}
	@RequestMapping({ "/displacement" })
	public String displacement(ModelMap model) {
		List displacements = this.baseService.findBySql("SELECT * FROM DISPLACEMENT WHERE TYPE=0");
		model.put("displacements", displacements);
		return "/history/displacement";
	}
	
	@RequestMapping(value = { "/displacementdata" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String displacementData(int senid, String begin, String end) {
		String beginTime = begin + " 00:00:00";
		String endTime = end + " 23:59:59";
		System.out.println("senid=" + senid + "&beginTime=" + beginTime + "&endTime=" + endTime);
		String sql = "select * from displacementdata where sid=" + senid + " and createtime>='" + beginTime
				+ "' and createtime<='" + endTime + "' order by createtime";
		List datas = this.baseService.findBySql(sql);
		Map ret = createData(2, datas, 0.0F);
		return JsonUtils.writeObjToString(ret);
	}

	@RequestMapping({ "/osmometer" })
	public String osmometer(ModelMap model) {
		List osmometers = this.baseService.findAllObjects(Osmometer.class);
		model.put("osmometers", osmometers);
		return "/history/osmometer";
	}

	@RequestMapping(value = { "/osmometerdata" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String osmometerData(int senid, String begin, String end) {
		String beginTime = begin + " 00:00:00";
		String endTime = end + " 23:59:59";
		System.out.println("senid=" + senid + "&beginTime=" + beginTime + "&endTime=" + endTime);
		String sql = "select * from osmometerdata where sid=" + senid + " and createtime>='" + beginTime
				+ "' and createtime<='" + endTime + "' order by createtime";
		List datas = this.baseService.findBySql(sql);
		List osmometerMap = this.baseService.findBySql("select * from osmometer where id=" + senid);
		float baseShould = 0.0F;
		if ((osmometerMap != null) && (osmometerMap.size() > 0)) {
			Map osmometer = (Map) osmometerMap.get(0);
			baseShould = NumberUtils.toFloat(String.valueOf(osmometer.get("should")))
					- NumberUtils.toFloat(String.valueOf(osmometer.get("offer")));
		}
		Map ret = createData(1, datas, baseShould);
		return JsonUtils.writeObjToString(ret);
	}

	private Map<String, Object> createData(int sentype, List<Map> datas, float baseShould) {
		Map ret = new HashMap();
		List ds = new ArrayList();
		float min = 0.0F;
		float max = 0.0F;

		if (sentype == 2) {
			if ((datas != null) && (datas.size() > 0)) {
				Map tmp = (Map) datas.get(0);
				max = min = NumberUtils.toFloat(String.valueOf(tmp.get("change")));
			}

			for (Map m : datas) {
				long x = DateUtil.strToLong(String.valueOf(m.get("createtime")), "yyyy-MM-dd HH:mm:ss");
				float y = NumberUtils.toFloat(String.valueOf(m.get("change")));

				float val = y;
				if (max < val) {
					max = val;
				}
				if (min > val) {
					min = val;
				}
				DataVO dv = new DataVO();
				dv.setX(x);
				dv.setY(val);
				ds.add(dv);
			}
		} else {
			float should;
			if ((datas != null) && (datas.size() > 0)) {
				Map tmp = (Map) datas.get(0);
				should = NumberUtils.toFloat(String.valueOf(tmp.get("should")));
				float offer = NumberUtils.toFloat(String.valueOf(tmp.get("offer")));
				if (baseShould != 0.0F) {
					offer = should - baseShould;
				}
				float y = (float) (offer / 9.800000000000001D * 100.0D);
				max = min = y;
			}

			for (Map m : datas) {
				long x = DateUtil.strToLong(String.valueOf(m.get("createtime")), "yyyy-MM-dd HH:mm:ss");
				float should1 = NumberUtils.toFloat(String.valueOf(m.get("should")));
				float offer = NumberUtils.toFloat(String.valueOf(m.get("offer")));
				if (baseShould != 0.0F) {
					offer = should1 - baseShould;
				}
				float y = (float) (offer / 9.800000000000001D * 100.0D);
				if (max < y) {
					max = y;
				}
				if (min > y) {
					min = y;
				}
				DataVO dv = new DataVO();
				dv.setX(x);
				dv.setY(y);
				ds.add(dv);
			}
		}
		ret.put("min", Float.valueOf(min));
		ret.put("max", Float.valueOf(max));
		ret.put("data", ds);
		return ret;
	}
}